package com.xmut.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.commonutils.R;
import com.xmut.eduservice.entity.EduChapter;
import com.xmut.eduservice.entity.EduVideo;
import com.xmut.eduservice.entity.chapter.ChapterVo;
import com.xmut.eduservice.service.EduChapterService;
import com.xmut.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;

    // 课程大纲列表
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    // 添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    // 根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }
    // 修改章节
    @PostMapping("updataChapter")
    public R updataChapter(@RequestBody EduChapter eduChapter) {
        boolean b = eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    // 删除
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        eduChapterService.deleteChapter(chapterId);
        return R.ok();
    }

    // 根据章节id查询所有小节
    @ApiOperation(value = "根据章节id查询所有该章节的video")
    @GetMapping("getAllVideoByChapterId/{chapterId}")
    public R getAllVideoByChapterId(@PathVariable String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        List<EduVideo> list = eduVideoService.list(queryWrapper);
        return R.ok().data("list", list);
    }


}

